package com.wbc.smartHome.web;

import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.model.Commodity;
import com.wbc.smartHome.model.CommodityLabel;
import com.wbc.smartHome.model.CommodityType;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.service.CommodityLabelService;
import com.wbc.smartHome.service.CommodityService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(path = "/api/commodityLabelCtl")
/*热门商品标签存取*/
@Api(tags = "商品标签模块")
public class CommodityLabelController {


    @Autowired
    private CommodityLabelService commodityLabelService;
    @Autowired
    private CommodityService commodityService;

    @ApiOperation("获取所有商品标签")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "getCommodityLabel")
    public Result getCommodityLabel(@RequestBody CommodityLabel commodityLabel) {
        PageInfo<CommodityLabel> commodityLabelPageInfo = commodityLabelService.getCommodityLabel(commodityLabel);
        return new Result(200, "OK", commodityLabelPageInfo);
    }

    @ApiOperation("添加商品标签")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "setCommodityLabel")
    public Result setCommodityLabel(@RequestBody CommodityLabel commodityLabel,@RequestHeader("adminToken") String adminToken) {
        if (commodityLabel.getId()==null){
            commodityLabelService.setCommodityLabel(commodityLabel,adminToken);
        }
        else {
            commodityLabelService.updateCommodityLabel(commodityLabel,adminToken);
        }

        return new Result(200,"ok",null);
    }

    @ApiOperation("删除商品标签")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "删除成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "delete")
    public Result delete(int id){
        commodityLabelService.delete(id);
        Result result = new Result(200, "删除成功", null);
        return result;
    }

    @ApiOperation("按照id查询商品标签")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "查询成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommodityLabelById")
    public Result getCommodityLabelById(int id){
        CommodityLabel commodityLabel = commodityLabelService.getCommodityLabelById(id);
        Result result = new Result(200, "查询成功", commodityLabel);
        return result;
    }



}
